<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/4/1
 * Time: 15:54
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Model\AlbumTemplateCategoryModel;
use SDXApi\Model\SeminarTemplateCategoryGoodsModel;
use SDXApi\Repositories\GoodsRepository;

/**
 * Class SeminarCategoryGoodsService
 * @package SDXApi\Service
 */
class AlbumTemplateCategoryService extends TemplateAbstract
{
    /**
     * @var SeminarTemplateCategoryGoodsModel
     */
    protected $seminarCategoryGoodsModel;

    protected $albumCategoryModel;

    /**
     * @var GoodsRepository
     */
    protected $goodsRepository;

    /**
     * SeminarCategoryGoodsService constructor.
     */
    public function __construct()
    {
        $this->seminarCategoryGoodsModel = new SeminarTemplateCategoryGoodsModel();
        $this->goodsRepository = new GoodsRepository();
        $this->albumCategoryModel = new AlbumTemplateCategoryModel();
    }

    /**
     * Created by Jerry
     * @param array $template
     * @param int $userId
     * @param int $wid
     * @return array|bool
     */
    public function handle($template = array(), $userId = 0, $wid = 8)
    {
        return $this->categoryGoods($template, $userId, $wid);
    }

    /**
     * 处理分类数据
     * Created by Jerry
     * @param array $page
     * @param int $userId
     * @param int $wid
     * @return array|bool
     */
    protected function categoryGoods($page = array(), $userId = 0, $wid = 8)
    {
        if (empty($page)) {
            return false;
        }

        $categoryInfo = $this->findCategoryByStencilId($page['id']);

        $goods = $this->checkGoodsCategory($categoryInfo, $userId, $wid, $page['id']);

        if (empty($goods)) {
            $goods = array();
        }

        $page['goods_list'] = $goods;
        $page['type'] = 2;

        return $page;
    }

    /**
     * Created by Jerry
     * @param int $id
     * @return bool|mixed
     */
    protected function findCategoryByStencilId($id = 0)
    {
        if (empty($id)) {
            return false;
        }

        return $this->albumCategoryModel->findCategoryByTemplateId($id);
    }

    /**
     * Created by Jerry
     * @param array $category
     * @param int $userId
     * @param int $wid
     * @param int $templateId
     * @return array|mixed
     */
    protected function checkGoodsCategory($category = array(), $userId = 0, $wid = 8, $templateId = 0)
    {
        $goodsList = $this->getCategoryGoodsCache($templateId, $category, $userId, $wid);

        foreach ($goodsList as $key => $value) {
            $goodsList[$key]['id'] = $value['goods_id'];
        }

        if (count($goodsList) > 1) {
            $goodsList = self::checkDataSort($goodsList);
        }

        $goodsList = array_merge($goodsList);

        return $goodsList;
    }

    /**
     * Created by Jerry
     * @param int $templateId
     * @param array $category
     * @param int $userId
     * @param int $wid
     * @return array
     */
    protected function getCategoryGoodsCache($templateId = 0, $category = array(), $userId = 0, $wid = 8)
    {
        $userRank = $this->getUserRank($userId);
        $cacheName = 'album_template_page_category_' . $templateId . '_rank_' . $userRank . '_wid_' . $wid;

        $goodsList = $this->getCache($cacheName);
        $this->goodsRepository->wid = $wid;
        $this->goodsRepository->userId = $userId;

        if (empty($goodsList)) {
            $goods = $this->findCategoryGoodsByCategoryId($category['category_id'], $category['goods_where'], $wid);
            $goodsList = $this->goodsRepository->formatGoodsTransform($goods);

            if (! empty($goodsList)) {
                $this->cacheData($cacheName, $goodsList, mt_rand(3600, 4200));
            }
        }

        return $this->goodsRepository->checkGoodsPractical($goodsList);
    }

    /**
     * Created by Jerry
     * @param int $category_id
     * @param int $goods_where
     * @param int $wid
     * @return bool|mixed
     */
    protected function findCategoryGoodsByCategoryId($category_id = 0, $goods_where = 0, $wid = 8)
    {
        return $this->goodsRepository->findCategoryGoodsByCategoryId($category_id, $goods_where, $wid);
    }
}